package com.cql.web.controller.business;

import com.cql.common.constant.FoodCircleConstant;
import com.cql.common.core.DO.UserDO;
import com.cql.common.core.DTO.RegisterDTO;
import com.cql.common.core.base.AjaxResult;
import com.cql.common.core.base.BaseController;
import com.cql.framework.config.security.service.LoginService;
import com.cql.service.business.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cql
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private LoginService loginService;


    /**
     * 完成注册并登录
     *
     * @param register
     * @return
     */
    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterDTO register) {
        AjaxResult ajax = AjaxResult.success();

        UserDO user = userService.insertUser(register);

        String token = loginService.login(user.getAccount(), register.getPassword());

        ajax.put(FoodCircleConstant.TOKEN, token);

        return ajax;
    }


}